package com.shop.cloud.service.hongfu.impl;

import java.sql.Timestamp;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.shop.cloud.common.mybatis.BaseService;
import com.shop.cloud.common.mybatis.MybatisDao;
import com.shop.cloud.dao.hongfu.SafetyCodeDao;
import com.shop.cloud.domain.hongfu.EncodeUtil;
import com.shop.cloud.domain.hongfu.SafetyCode;
import com.shop.cloud.service.hongfu.ISafetyCodeService;

/**
 * 
 */
@Service("safetyCodeService")
public class SafetyCodeService extends BaseService<SafetyCode> implements ISafetyCodeService{
    
    @Autowired
	private SafetyCodeDao safetyCodeDao;
    
    @Autowired
    private SafetyCodeService safetyCodeService;

	@Override
	public MybatisDao<SafetyCode> getEntityDao() {
		return safetyCodeDao;
	}

	@Override
	public int handleCode(Long userId, SafetyCode safetyCode) {
		// TODO Auto-generated method stub
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("userId", userId);
		SafetyCode safetyCode2 =safetyCodeService.findOne(params);
		int flag = 0;
		//是否有安全码
		if (null ==safetyCode2) {
			
			safetyCode.setUserId(userId);
			safetyCode.setPassword(EncodeUtil.encodeMD5(safetyCode.getPassword()));
			safetyCode.setCreateTime(new Timestamp((new Date()).getTime()));
			//默认：0
			safetyCode.setStatus(0);
			flag = safetyCodeDao.insert(safetyCode);
		}
		return flag;
	}

	@Override
	public int handleCheckCode(Long userId, SafetyCode safetyCode) {
		// TODO Auto-generated method stub
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("userId", userId);
		SafetyCode safetyCode2 =safetyCodeService.findOne(params);
		if (safetyCode2.getPassword().equals(EncodeUtil.encodeMD5(safetyCode.getPassword()))) {
			return 1;
		}
		return 0;
	}

	@Override
	public int handleQueryCode(Long userId, SafetyCode safetyCode) {
		// TODO Auto-generated method stub
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("userId", userId);
		safetyCode =safetyCodeService.findOne(params);
		if (safetyCode != null) {
			return 1;
		}
		return 0;
	}

	public int HandleDeleteCode(SafetyCode safetyCode) {
		// TODO Auto-generated method stub
		return safetyCodeDao.HandleDeleteCode(safetyCode);
	}
    
}